Using T[1] instead of T for functions overloaded for T(&)[N]

Posted by Abyx on Programmers See other posts from Programmers or by Abyx
Published on 2012-09-26T19:30:13Z Indexed on 2012/09/26 21:49 UTC
Read the original article Hit count: 192

Filed under:
|
|

The asio::buffer function has (void*, size_t) and (PodType(&)[N]) overloads.
I didn't want to write ugly C-style (&x, sizeof(x)) code, so I wrote this:

SomePacket packet[1]; // SomePacket is POD
read(socket, asio::buffer(packet));
foo = packet->foo;

But that packet-> looks kinda weird - the packet is an array after all.
(And packet[0]. doesn't look better.)

Now, I think if it was a good idea to write such code. Maybe I should stick to unsafe C-style code with void* and sizeof?


Upd: here is another example, for writing a packet:

SomePacket packet[1]; // SomePacket is POD
packet->id = SomePacket::ID;
packet->foo = foo;
write(socket, asio::buffer(packet));

© Programmers or respective owner

Related posts about c++

Related posts about code-style